import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Controls.Material 2.15
import QtGraphicalEffects 1.15

ItemDelegate {
    id: card
    width: Math.min(350, ListView.view.width * 0.85)
    height: 110  // 增加高度以容纳capabilities显示
    padding: 0
    highlighted: true

    property string robotType: ""
    property string robotId: ""
    property bool isOnline: true
    property string status: ""
    property string battery: ""
    property string robotIP: ""
    property string seerIP: ""
    property string cameraUrl: ""
    property var capabilities: []  // 添加capabilities属性

    Component.onCompleted:{
        console.log("robotId: " + robotId + " " + robotType + " " + isOnline + " " + status + " " + battery + " "  + robotIP + " " + seerIP + " " + cameraUrl);
    }

    // 悬停动画
    HoverHandler {
        cursorShape: Qt.PointingHandCursor
        onHoveredChanged: card.scale = hovered ? 0.98 : 1.0
    }
    Behavior on scale { NumberAnimation { duration: 150 } }

    // 简约卡片设计 - 使用Material风格
    Rectangle {
        anchors.fill: parent
        radius: 12
        color: Material.background // 使用Material背景色
        layer.enabled: true
        layer.effect: DropShadow {
            radius: 12
            samples: 16
            color: "#20000000"
            verticalOffset: 2
        }

        // 左侧状态条 - 使用Material颜色
        Rectangle {
            width: 6
            height: parent.height
            radius: 3
            color: isOnline ? Material.color(Material.Green) : Material.color(Material.Red)
        }

        // 内容布局
        Row {
            anchors {
                left: parent.left
                right: parent.right
                top: parent.top
                margins: 16
                leftMargin: 24
            }
            height: parent.height - 32
            spacing: 16

            // 图标区 - 使用Material颜色
            Rectangle {
                width: 40
                height: 40
                radius: 20
                color: isOnline ? Material.color(Material.Green, Material.Shade50) : Material.color(Material.Red, Material.Shade50)
                border.width: 1
                border.color: isOnline ? Material.color(Material.Green, Material.Shade200) : Material.color(Material.Red, Material.Shade200)

                Text {
                    text: "🤖"
                    anchors.centerIn: parent
                    font.pixelSize: 24
                }
            }

            // 文字信息
            Column {
                spacing: 4
                width: parent.width - 80

                Text {
                    text: robotId
                    font.bold: true
                    font.pixelSize: 16
                    color: Material.foreground // 使用Material前景色
                    elide: Text.ElideRight
                }

                Text {
                    text: {
                        const types = {
                            "inspection": "巡检机器人",
                            "harvest": "采摘机器人",
                            "weed": "除草机器人",
                            "grafting": "嫁接机器人",
                            "transport": "运输机器人",
                            "guide": "讲解机器人"
                        };
                        return "类型：" + (types[robotType] || robotType);
                    }
                    font.pixelSize: 13
                    color: Material.color(Material.Grey, Material.Shade600) // 使用Material灰色
                }

                Text {
                    text: "状态：" + status  + "  电量: " + battery + "%"
                    font.pixelSize: 11
                    color: {
                        if (status.includes("故障")) return Material.color(Material.Red); // 故障状态红色
                        else if (status.includes("忙碌")) return Material.color(Material.Orange); // 忙碌状态橙色
                        else return Material.color(Material.Grey, Material.Shade500); // 默认灰色
                    }
                    font.bold: status.includes("紧急") // 紧急状态加粗
                }

                // 添加capabilities显示
                Text {
                    text: "支持任务类型：" + (capabilities.length > 0 ? capabilities.join(", ") : "无")
                    font.pixelSize: 10
                    color: Material.color(Material.Grey, Material.Shade400)
                    elide: Text.ElideRight
                    width: parent.width
                }
            }

            // 状态指示器 - 使用Material颜色
            Rectangle {
                width: 16
                height: 16
                radius: 8
                color: isOnline ? Material.color(Material.Green) : Material.color(Material.Red)
                anchors.verticalCenter: parent.verticalCenter

                // 呼吸动画
                SequentialAnimation on opacity {
                    running: isOnline
                    loops: Animation.Infinite
                    NumberAnimation { to: 0.6; duration: 800; }
                    NumberAnimation { to: 1.0; duration: 800; }
                }
            }
        }
    }

    // 点击事件跳转到详情页面
    onClicked: {
        if (stackView) {
            stackView.push(Qt.resolvedUrl("qrc:/qml/pages/RobotMonitorPage.qml"), { 
                robotId: robotId,
                robotIP: robotIP,
                seerIP: seerIP,
                cameraUrl: cameraUrl,
                robotCapabilities: capabilities,
                stackView: stackView //传递stackView引用
            })
        }
    }
}